package com.usian.comment.service.impl;

import com.usian.comment.service.ApCommentLikeService;
import com.usian.model.comment.dtos.CommentLikeDto;
import com.usian.model.comment.pojos.ApComment;
import com.usian.model.comment.pojos.ApCommentLike;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class ApCommentLikeServiceImpl implements ApCommentLikeService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ResponseResult like(CommentLikeDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getCommentId()) || dto.getOperation() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //根据id查询评论的信息
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if (apComment != null) {
            // 点赞
            if (dto.getOperation() == 0) {
                Update update = new Update();
                update.set("likes", apComment.getLikes() + 1);
                mongoTemplate.upsert(Query.query(Criteria.where("id").is(dto.getCommentId())), update, ApComment.class);

                ApCommentLike apCommentLike = new ApCommentLike();
                apCommentLike.setAuthorId(apUser.getId());
                apCommentLike.setCommentId(dto.getCommentId());
                apCommentLike.setOperation(dto.getOperation());
                mongoTemplate.insert(apCommentLike);
            } else if (dto.getOperation() == 1) {
                //取消点赞
                Update update = new Update();
                update.set("likes", apComment.getLikes() <= 0 ? 0 : apComment.getLikes() - 1);
                mongoTemplate.upsert(Query.query(Criteria.where("id").is(dto.getCommentId())), update, ApComment.class);

                mongoTemplate.remove(Query.query(Criteria.where("authorId").is(apUser.getId()).and("commentId")
                        .is(dto.getCommentId())),ApCommentLike.class);
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //返回likes数量
        HashMap<String, Object> map = new HashMap<>();
        ApComment apComment1 = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        map.put("likes",apComment1.getLikes());
        return ResponseResult.okResult(map);
    }
}
